﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;
using TidyBlog.Entity;
using TidyBlog.DAL;

namespace TidyBlog.BLL
{
    /// <summary>
    /// 友链业务逻辑层
    /// </summary>
    public class LinkBLL
    {
        /// <summary>
        /// 缓存键名
        /// </summary>
        public const string CacheName = "LinkList";

        /// <summary>
        /// 验证友情链接属性
        /// </summary>
        /// <param name="link">友情链接</param>
        public static void Validate(Link link)
        {
            if (String.IsNullOrEmpty(link.LinkName))
            {
                throw new ArgumentException("友情链接名不能为空");
            }
            if (String.IsNullOrEmpty(link.LinkUrl))
            {
                throw new ArgumentException("友情链接地址不能为空");
            }
        }

        /// <summary>
        /// 创建友情链接
        /// </summary>
        /// <param name="link">友情链接</param>
        public static void Create(Link link)
        {
            Validate(link);

            try
            {
                LinkDAL.Create(link);
            }
            catch
            {
#if DEBUG
                throw;
#else
                throw new Exception("创建友情链接记录出错");
#endif
            }

            HttpContext.Current.Cache.Remove(CacheName);
        }

        /// <summary>
        /// 更新友情链接
        /// </summary>
        /// <param name="link">友情链接</param>
        public static void Update(Link link)
        {
            Validate(link);

            try
            {
                LinkDAL.Update(link);
            }
            catch
            {
#if DEBUG
                throw;
#else
                throw new Exception("更新友情链接记录出错");
#endif
            }

            HttpContext.Current.Cache.Remove(CacheName);
        }

        /// <summary>
        /// 读取友情链接
        /// </summary>
        /// <param name="linkId">友情链接编号</param>
        /// <returns>友情链接</returns>
        public static Link Read(int linkId)
        {
            var result = from g in List(0) where g.LinkId == linkId select g;

            return result.Count() > 0 ? result.First() : null;
        }

        /// <summary>
        /// 删除友情链接
        /// </summary>
        /// <param name="linkId"></param>
        public static void Delete(int linkId)
        {
            try
            {
                LinkDAL.Delete(linkId);
            }
            catch
            {
#if DEBUG
                throw;
#else
                throw new Exception("删除友情链接记录出错");
#endif
            }

            HttpContext.Current.Cache.Remove(CacheName);
        }

        /// <summary>
        /// 读取友情链接列表
        /// </summary>
        /// <param name="minWeight">最小权重（包括）</param>
        /// <returns>友情链接列表</returns>
        public static Link[] List(byte minWeight)
        {
            var list = HttpContext.Current.Cache[CacheName] as Link[];

            if (list == null)
            {
                try
                {
                    list = LinkDAL.List();
                }
                catch
                {
#if DEBUG
                throw;
#else
                    throw new Exception("读取友情链接记录出错");
#endif
                }

                // 写入缓存
                HttpContext.Current.Cache[CacheName] = list;
            }

            // 通过LINQ找出符合条件的数据并按权重降序排列
            return (from g in list where g.Weight >= minWeight select g).ToArray();
        }
    }
}
